<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;
class Page extends Base{
    public function list_data(){
        $listData = Db::table('an_page')->order('sort', 'desc')->select()->toArray(); 
        $this->assign('listData',json_encode($listData));
        return $this->fetch();
    }
    
    public function form_data($id=0){
        $pt = input("post.");
        if(count($pt)>0){
            if(intval($id)>0){
                $pt['url'] =  trim($pt['url']);
                $pt['cn_name'] =  trim($pt['cn_name']);
                $pt['en_name'] =  trim($pt['en_name']);
                $bool = Db::table('an_page')->cache('page_cache')->where('id', $pt['id'])->data($pt)->update();
                $this->logData('修改栏目信息:'.$pt['title']);
                if($bool == 1){
                    $this->info['code']=1;
                    $this->info['msg']='修改栏目成功';
                    $this->info['data']=$pt;
                }else{
                    $this->info['code']=0;
                    $this->info['msg']='修改栏目失败';
                    $this->info['data']=$pt;
                }
            }else{
                $pt['url'] =  trim($pt['url']);
                $pt['cn_name'] =  trim($pt['cn_name']);
                $pt['en_name'] =  trim($pt['en_name']);
                $bool = Db::table('an_page')->cache('page_cache')->insert($pt);
                $this->logData('新增栏目:'.$pt['title']);
                if($bool == 1){
                    $this->info['code']=1;
                    $this->info['msg']='新增栏目成功';
                    $this->info['data']=$pt;
                }else{
                    $this->info['code']=0;
                    $this->info['msg']='新增栏目失败';
                    $this->info['data']=$pt;
                }
            }
            return json($this->info);
        }
        if($id!=0){
            $pageData = Db::table('an_page')->where('id', $id)->find();
        }else{
            $pageData = [
                'sort'=>'',
                'cn_name'=>'',
                'url'=>'',
                'img'=>'',
                'content'=>'',
                'title'=>'',
                'min_title'=>'',
                'en_name'=>'',
                'pid'=>'',
                'keywords'=>'',
                'other'=>'',
                'description'=>'',
                'is_show'=>'',
                'type'=>''

            ];
        }
        $this->assign('pageData',$pageData);
        $pageList = Db::query("select * from an_page");
        $typeList = Db::query("select * from an_type");
        $this->assign('typeList',$typeList);
        $this->assign('pageList',$pageList);
        $this->assign('typeGroup',config('an_config.type'));
        $this->assign('id',$id);
        return $this->fetch();
    }


    public function del_page(){
        $pt = input("post.");
        $bool = Db::table('an_page')->cache('page_cache')->where('id',$pt['id'])->delete();
        $this->logData('删除菜单栏目:链接为'.$pt['url']);
        if($bool){
            $this->info['code'] = 1;
        }else{
            $this->info['msg']='操作失败';
            $this->info['code'] = 0;
        }
        return json($this->info);
    }

    public function up_sort(){
        $pt = input("post.");
        foreach($pt['val'] as $k=>$v){
            Db::table('an_page')->cache('page_cache')->where('id', $v['id'])->data($v)->update();
        }
        $this->logData('修改栏目排序');
        $this->info['code'] = 1;
        return json($this->info);
    }
}
